﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccessLayer;
using BussinessLayer;

namespace QLBaoHanh.QLTbiDangONCC
{
    public partial class frmLuuNhatKi : Form
    {
        private bool _isEditForm;
        private int _phien_ThietBiBHId;
        private int _lichSuId;
        private frmDsTbiDangONCC _callerForm;

        public frmLuuNhatKi(bool inIsEditForm, int inPhien_ThietBiBHId, frmDsTbiDangONCC inCallerForm, int inLichSuId = 0)
        {
            InitializeComponent();
            _isEditForm = inIsEditForm;
            _phien_ThietBiBHId = inPhien_ThietBiBHId;
            _lichSuId = inLichSuId;
            _callerForm = inCallerForm;
        }

        private void FillForm()
        {
            if(_isEditForm)
            {
                this.Text = "Sửa nhật kí sửa chữa";
                //get and fill data to form
                LichSuBH obj = LichSuBHBO.getInstance.GetLichSuBHById(_lichSuId);
                txtNoiDung.Text = obj.NoiDung;
                txtChiPhi.Text = obj.ChiPhi.ToString();
            }
            else
            {
                //
                this.Text = "Thêm nhật kí sửa chữa";
                txtChiPhi.Text = "0";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            LichSuBH obj;
            double ChiPhiMoi = 0;
            double ChiPhiCu = 0;
            ChiPhiMoi= Convert.ToInt32(txtChiPhi.Text);
            if (_isEditForm)
            {
                //Update lich su bao hanh
                obj = LichSuBHBO.getInstance.GetLichSuBHById(_lichSuId);
                ChiPhiCu = (double)obj.ChiPhi;
                
                obj.NVThucHienId = frmMain.CurrentNhanVien.Id;
                obj.NoiDung = txtNoiDung.Text;
                obj.ChiPhi = ChiPhiMoi;
                LichSuBHBO.getInstance.UpdateLichSuBH(obj);
                //Ghi nhan chi phi
                Phien_ThietBiBH ThisThietBi = Phien_ThietBiBHBO.getInstance.GetPhien_ThietBiBHById(_phien_ThietBiBHId);
                ThisThietBi.ChiPhi = ThisThietBi.ChiPhi - ChiPhiCu + ChiPhiMoi;
                Phien_ThietBiBHBO.getInstance.UpdatePhien_ThietBiBH(ThisThietBi);
            } 
            else
            {
                //Them moi lich su
                obj = new LichSuBH();
                obj.LoaiLichSu = Convert.ToInt16(MyCommon.LoaiLichSu.LichSuSuaChua);
                obj.Phien_ThietBiBHId = _phien_ThietBiBHId;
                obj.ThoiGian = DateTime.Now;
                obj.NVThucHienId = frmMain.CurrentNhanVien.Id;
                obj.NoiDung = txtNoiDung.Text;
                obj.ChiPhi = ChiPhiMoi;
                LichSuBHBO.getInstance.InsertLichSuBH(obj);
                //Ghi nhan chi phi
                Phien_ThietBiBH ThisThietBi = Phien_ThietBiBHBO.getInstance.GetPhien_ThietBiBHById(_phien_ThietBiBHId);
                if (ThisThietBi.ChiPhi == null)
                {
                    ThisThietBi.ChiPhi = 0;
                }
                ThisThietBi.ChiPhi += ChiPhiMoi;
                Phien_ThietBiBHBO.getInstance.UpdatePhien_ThietBiBH(ThisThietBi);
            }
            //_callerForm.LoadGridNhatKiBaoHanh(_phien_ThietBiBHId);
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmLuuNhatKi_Load(object sender, EventArgs e)
        {
            FillForm();
        }
    }
}
